//
//  MOFSCollectionView.m
//  fitnessmanage
//
//  Created by wujian on 2017/11/27.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "MOFSCollectionView.h"
#import "MOFSCollectionViewCell.h"

#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface MOFSCollectionView()<UICollectionViewDataSource,UICollectionViewDelegate>

//团体
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) NSMutableArray *dataFlagArr;
//私教
@property (nonatomic, strong) NSMutableArray *dataArr1;

@property (nonatomic, strong) NSMutableArray *dataFlageArr1;

@property (nonatomic, strong) NSMutableArray *sectionFlag;

@property (nonatomic, strong) UIView *bgView;

@end

@implementation MOFSCollectionView

- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    return _dataArr;
}

-(NSMutableArray *)dataArr1{

    if (!_dataArr1) {
        
        _dataArr1 = [[NSMutableArray alloc] init];
    }
    
    return _dataArr1;
}

-(NSMutableArray *)sectionFlag{

    if (!_sectionFlag) {
        
        _sectionFlag = [[NSMutableArray alloc] init];
        
        [_sectionFlag addObject:@(YES)];
        
        [_sectionFlag addObject:@(YES)];
    }
    
    return _sectionFlag;
}

-(id)initWithFrame:(CGRect)frame{
    
    
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        self.backgroundColor = CSClearColor;
        
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
        [self initPickerView];
        
        [self initContainerView];
        
        [self initBgView];
    }
    
    return self;
}

- (void)initContainerView {
    
    self.containerView = [[UIView alloc] init];
    
    self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    
    self.containerView.userInteractionEnabled = YES;
    
    [self.containerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenWithAnimation)]];
    
    [self addSubview:self.containerView];
    
    [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}

- (void)initBgView {
    
    //白色背景
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = CSWhiteColor;
    [self addSubview:self.bgView];
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-20);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = BoldBigFont;
    [self.bgView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgView).offset(20);
        make.centerX.mas_equalTo(self.bgView);
    }];
    
    UIButton *closeBtn = [[UIButton alloc] init];
    [closeBtn setImage:@"pop_icon_close"];
    [closeBtn addTarget:self action:@selector(hiddenWithAnimation)];
    [self.bgView addSubview:closeBtn];
    self.closeBtn = closeBtn;
    [closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bgView).offset(-10);
        make.top.mas_equalTo(self.bgView).offset(10);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = LineBackGroundColor;
    [self.bgView addSubview:lineView];
    self.lineView = lineView;
    [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(30);
        make.left.width.mas_equalTo(self.bgView);
        make.height.mas_equalTo(1);
    }];
    
    [self.bgView addSubview:self.collectionView];
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lineView.mas_bottom);
        make.left.mas_equalTo(self.bgView).offset(30);
        make.right.mas_equalTo(self.bgView).offset(-30);
        make.height.mas_equalTo(300);
    }];
    
    MOFSBottomView *bottomView = [[MOFSBottomView alloc] init];
    [self.bgView addSubview:bottomView];
    self.bottomView = bottomView;
    [bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.collectionView.mas_bottom);
        make.left.width.bottom.mas_equalTo(self.bgView);
    }];
}

-(void)initPickerView{
    
    // 初始化CollectionView
    UICollectionViewFlowLayout *collectViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置cell之间间距
    collectViewFlowLayout.minimumInteritemSpacing = 0;
    //设置行距
    collectViewFlowLayout.minimumLineSpacing = 10;
    collectViewFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    CGFloat itemWith = (SCREEN_WIDTH - 40 - 60) / 2;
    //设置Item大小
    collectViewFlowLayout.itemSize = CGSizeMake(itemWith, 80);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.frame collectionViewLayout:collectViewFlowLayout];
    [self.collectionView registerClass:[MOFSCollectionViewCell class] forCellWithReuseIdentifier:@"MOFSCollectionViewCell"];
    [self.collectionView registerClass:[MOFSHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MOFSHeaderReusableView"];        
    //指定数据源和委托
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = CSWhiteColor;
    self.collectionView.showsVerticalScrollIndicator = NO;
}

#pragma mark - Action
- (void)showMOFSCollectionViewWithDataArray:(NSArray *)array otherArray:(NSArray *)array1 commitBlock:(void(^)(NSArray *commArray,NSArray *privateArray))commitBlock cancelBlock:(void(^)(void))cancelBlock{
    
    if (!self.dataFlagArr || ![self.dataArr isEqualToArray:array]) {
        
        self.dataFlagArr = [[NSMutableArray alloc] init];
        
        for (int i = 0 ;i < array.count; i++) {
            
            [self.dataFlagArr addObject:@(NO)];
        }
    }
    
    self.dataArr = [NSMutableArray arrayWithArray:array];
    
    
    
    if (!self.dataFlageArr1 || ![self.dataArr1 isEqualToArray:array1]) {
        
        self.dataFlageArr1 = [[NSMutableArray alloc] init];
        
        for (int i = 0 ;i < array1.count; i++) {
            
            [self.dataFlageArr1 addObject:@(NO)];
        }
    }
    
    self.dataArr1 = [NSMutableArray arrayWithArray:array1];
    
    [self.collectionView reloadData];
    
    [self showWithAnimation];
    
    __weak __typeof(self) weakSelf = self;
    
    self.bottomView.cancelBlock = ^ {
        
        if (cancelBlock) {
            
            [weakSelf hiddenWithAnimation];
            
            cancelBlock();
        }
    };
    
    self.bottomView.commitBlock = ^ {
        [weakSelf hiddenWithAnimation];
        
        if (commitBlock) {
            
            NSMutableArray *commArray = [[NSMutableArray alloc] init];
            for (int i = 0; i < weakSelf.dataFlagArr.count; i++) {
                BOOL flage = [[weakSelf.dataFlagArr objectAtIndex:i] boolValue];
                if (flage) {
                 
                    [commArray addObject:[weakSelf.dataArr objectAtIndex:i]];
                }
            }
            
            NSMutableArray *privateArray = [[NSMutableArray alloc] init];
            for (int i = 0; i < weakSelf.dataFlageArr1.count; i++) {
                BOOL flage = [[weakSelf.dataFlageArr1 objectAtIndex:i] boolValue];
                if (flage) {
                    [privateArray addObject:[weakSelf.dataArr1 objectAtIndex:i]];
                }
            }
            
            commitBlock(commArray,privateArray);
        }
    };
}

- (void)showWithAnimation {
    
    [self addViews];
    
    self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    
    CGFloat height = self.bgView.frame.size.height;
    
    self.bgView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT + height / 2);
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.bgView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT - height / 2);
        
        self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
    
}

- (void)hiddenWithAnimation {
    CGFloat height = self.bgView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.bgView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT + height / 2);
        self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
                
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    [self removeFromSuperview];
}

#pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    BOOL flage = [[self.sectionFlag objectAtIndex:section] boolValue];
    
    if (section == 0) {
        
        return flage? self.dataArr.count:0;
    }
    
    else{
        
        return flage? self.dataArr1.count : 0;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MOFSCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MOFSCollectionViewCell" forIndexPath:indexPath];
    
    NSString *title;
    
    BOOL flage ;
    
    if (indexPath.section == 0) {
     
        title = [self.dataArr objectAtIndex:indexPath.row];
        
        flage = [[self.dataFlagArr objectAtIndex:indexPath.row] boolValue];
    }
    else{
        
        title = [self.dataArr1 objectAtIndex:indexPath.row];
        
        flage = [[self.dataFlageArr1 objectAtIndex:indexPath.row] boolValue];
    }

    cell.checkBtn.selected = flage;
    
    cell.titleLabel.text = title;
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
     
        BOOL flage = [[self.dataFlagArr objectAtIndex:indexPath.row] boolValue];
        
        [self.dataFlagArr replaceObjectAtIndex:indexPath.row withObject:@(!flage)];
    }
    else{
        
        BOOL flage = [[self.dataFlageArr1 objectAtIndex:indexPath.row] boolValue];
        
        [self.dataFlageArr1 replaceObjectAtIndex:indexPath.row withObject:@(!flage)];
    }
    
    [self.collectionView reloadSections:[[NSIndexSet alloc]initWithIndex:indexPath.section]];
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    MOFSHeaderReusableView *headView = nil;
    
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        
        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MOFSHeaderReusableView" forIndexPath:indexPath];
    }
    
    if (indexPath.section == 0) {
    
        headView.titleLabel.text = @"团体";
        
    }
    else{
        
        headView.titleLabel.text = @"私教";
    }
    
    BOOL flage = [[self.sectionFlag objectAtIndex:indexPath.section] boolValue];
    headView.choseBtn.selected = flage;
    headView.headBtn.tag = indexPath.section;
    [headView.headBtn addTarget:self action:@selector(sectionHeadClick:)];
    headView.section = indexPath.section;
    
    return headView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(self.width, 60);
    
}


#pragma mark SectionHeadClick
-(void)sectionHeadClick:(UIButton *)btn{
    
    NSInteger section = btn.tag;
    
    BOOL flage = [[self.sectionFlag objectAtIndex:section] boolValue];
    
    [self.sectionFlag replaceObjectAtIndex:section withObject:@(!flage)];
    
    [self.collectionView reloadSections:[[NSIndexSet alloc]initWithIndex:section]];
}

-(void)dellocMOFSCollectionView{
    
    [self.dataArr1 removeAllObjects];
    [self.dataFlageArr1 removeAllObjects];
    [self.dataArr removeAllObjects];
    [self.dataFlagArr removeAllObjects];
}
@end
